package gw.com.android.ui;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import gw.com.android.model.ConfigType;
import www.com.library.app.PushMsgTabFragment;
import www.com.library.model.DataItemResult;

/**
 * Created by reeta.zou on 2016/7/26.
 * 跟tablayout关联使用的viewpager的适配器
 */
public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    private DataItemResult mTabTitles;
    private ShowFragmentListener mListener;

    public ViewPagerAdapter(FragmentManager fm, DataItemResult tabTitles, ShowFragmentListener listener) {
        super(fm);
        this.mTabTitles = tabTitles;
        this.mListener = listener;
    }

    @Override
    public int getCount() {
        return null == mTabTitles ? 0 : mTabTitles.getDataCount();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if(position >= 0 && position < mTabTitles.getDataCount()){
            return mTabTitles.getItem(position).getString(ConfigType.CONFIG_TYPE_TITLE_TAG);
        }
        return "";
    }

    @Override
    public PushMsgTabFragment getItem(int position) {
        String type = mTabTitles.getItem(position).getString(ConfigType.CONFIG_TYPE_TYPE_TAG);
        return mListener.showFragment(type, position);
    }

    public interface ShowFragmentListener{
        PushMsgTabFragment showFragment(String type, int position);
    }

}

